package com.hyx.java.ctrl.admin;

import cn.hutool.core.util.StrUtil;
import com.hyx.java.base.BaseResult;
import com.hyx.java.base.PageBean;
import com.hyx.java.dto.HyxUserAddReq;
import com.hyx.java.dto.HyxUserResetReq;
import com.hyx.java.dto.HyxUserSearchReq;
import com.hyx.java.dto.HyxUserUpdateReq;
import com.hyx.java.entity.HyxUser;
import com.hyx.java.service.HyxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/user")
@CrossOrigin(origins = "*")
@Api(value = "用户管理",tags = "管理端接口")
public class HyxUserCtrl {

    @Autowired
    private HyxUserService userService;
    @ApiOperation(value = "用户列表")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public BaseResult<PageBean<HyxUser>> userPage(@RequestBody HyxUserSearchReq search) {
        return BaseResult.success(userService.userPage(search));
    }

    @ApiOperation(value = "用户新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult add(@RequestBody HyxUserAddReq user) {
        if (user == null || StrUtil.isEmpty(user.getUserName())) {
            return BaseResult.fail("011", "指定参数为空");
        }
        return userService.add(user);
    }

    @ApiOperation(value = "用户修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult update(@RequestBody HyxUserUpdateReq user) {
        if (user == null || user.getId() == null || StrUtil.isEmpty(user.getUserName())) {
            return BaseResult.fail("011", "指定参数为空");
        }
        return userService.update(user);
    }

    @ApiOperation(value = "用户删除")
    @RequestMapping(value = "/delete/", method = RequestMethod.POST)
    public BaseResult delete(@RequestBody HyxUserResetReq user) {
        if (user == null || user.getId() == null) {
            return BaseResult.fail("011", "指定参数为空");
        }
        return userService.delete(user.getId());
    }

    @ApiOperation(value = "用户重置密码")
    @RequestMapping(value = "/reset-password", method = RequestMethod.POST)
    public BaseResult resetPassword(@RequestBody HyxUserResetReq user) {
        if (user == null || user.getId() == null) {
            return BaseResult.fail("011", "指定参数为空");
        }
        return userService.resetPassword(user.getId());
    }


}
